package com.os.meituan.common;


import com.os.meituan.service.sec.RedisCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by Administrator on 2017/5/26.
 */
@Service
public class PubClass {

    @Value("${sysName}")
    private String g_sysName;
    @Resource
    private RedisCacheService redisCacheService;

    public Pageable createPagenation() {
        return createPagenation("id");
    }
    public Pageable createPagenation(String sortKey) {
        return createPagenation(Sort.Direction.DESC, sortKey);
    }
    public Pageable createPagenation(Sort.Direction dir, String sortKey) {
        int page = WebClass.requestToInt("pageNo");
        int pageSize = WebClass.requestToInt("pageSize");

        if (page > 0 && pageSize > 0) {
            return PageRequest.of(page - 1, pageSize, createPagenationSort(dir, sortKey));
        } else {
//            return Pageable.unpaged();
//            return new UnpagedSort(createPagenationSort(dir, sortKey));
            return PageRequest.of(0, 100000000, createPagenationSort(dir, sortKey));
        }
    }

    public Sort createPagenationSort(Sort.Direction dir, String sortKey) {
        return Sort.by(dir, sortKey);
    }

    public void showErrorLog(String content, Class<?> clazz) {
        Logger logger = LoggerFactory.getLogger(clazz);
        logger.error(content);
    }

    public void setCache(String key, Object obj, int mins) {
        redisCacheService.set(fmtCacheKey(key), obj, mins);
    }

    private String fmtCacheKey(String key) {
        return g_sysName + "_" + key;
    }

    public void showLog(String content) {
        Logger logger = LoggerFactory.getLogger(PubClass.class.getName());
        logger.info(content);
    }
}
